package com.example.springdatajpa.controller;

import com.example.springdatajpa.entity.Order;
import com.example.springdatajpa.service.OrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

@RestController
public class OrderController {
    @Autowired
    private OrderService service;

    @ApiOperation(value = "保存用户信息",notes = "")
    @RequestMapping("save")
    public String save(){
        Order order = new Order();
        order.setId(1234L);
        order.setNote("测试4");
        service.sava(order);
        return "保存成功";
    }

    @ApiOperation(value = "查找订单信息",notes = "根据note属性查找订单信息")
    @ApiImplicitParam(name = "note",value = "备注信息",required = true,dataType = "String")
    @RequestMapping("selectByNote/{str}")
    public Object findByNoteIs(@PathVariable("str") String str){
        System.out.println(str);
        return service.findByNoteIs(str);
    }

    @ApiOperation(value = "查找订单信息",notes = "利用JPQL的方式根据两个参数查找订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "note",value = "备注信息",required = true,dataType = "String"),
            @ApiImplicitParam(name = "id",value = "订单id",required = true,dataType = "Long")
    })
    @RequestMapping("queryOrderByNoteUseJPQL/{str}/{id}")
    List<Order> queryOrderByNoteUseJPQL(@PathVariable("str") String name,
                                        @PathVariable("id") Long id){
        return service.queryOrderByNoteUseJPQL(name,id);
    }


    @RequestMapping("queryOrderByNoteUseSQL/{str}/{id}")
    List<Order>queryOrderByNoteUseSQL(@PathVariable("str") String note,
                                      @PathVariable("id") Long id){
        return service.queryOrderByNoteUseSQL("这是一个备注",id);
    }

    @RequestMapping("updateUseById/{note}/{id}")
    public String updateUseById(@PathVariable("note") String note,@PathVariable("id") Long id){
        service.updateUseById(note,id);
        System.out.println(id);
        return "修改成功";
    }

    @RequestMapping("saveBy2")
    public Object saveByRepostiry(){
        Order order = new Order();
        order.setNote("CrudRepository操作");
        service.save(order);
        return "保存成功";

    }

    @RequestMapping("saveByList")
    public Object saveByRepostiry2(){
        Order order = new Order();
        order.setNote("CrudRepository操作");
        Order order2 = new Order();
        order.setNote("CrudRepository操作2");
        Order order3 = new Order();
        order.setNote("CrudRepository操作3");
        List<Order> orders = new LinkedList<>();
        orders.add(order);
        orders.add(order2);
        orders.add(order3);
        return "保存成功";
    }

    @RequestMapping("findByPage/{page}/{size}")
    public List<Order> findByPage(@PathVariable("page")Integer page,
                                  @PathVariable("size")Integer size){
        Page<Order> p = service.page1(page,size);
        System.out.println("总条数："+p.getTotalElements());
        System.out.println("总页数："+p.getTotalPages());
        System.out.println(p.getNumber());
        for (Order o:p.getContent()
             ) {
            System.out.println(o);
        }
        return p.getContent();
    }

    @RequestMapping("findBySort")
    public List<Order>findByOrder(){
        return service.findSort();
    }

    @RequestMapping("findByMoreSort")
    public List<Order>findByMoreOrder(){
        return service.findMoreSort();
    }

    @RequestMapping("findBySpecification")
    public List<Order> findBySpecification(){
        return service.findBySpecification();
    }

    @RequestMapping("findBySpecificationMore")
    public List<Order> findBySpecificationMore(){
        return service.findBySpecificationMore();
    }

    @RequestMapping("findByPage")
    public List<Order> findByPage(){
        return service.findByPage();
    }

    @RequestMapping("findBySortBySpecification")
    public List<Order>findBySortBySpecification(){
        return service.findBySortBySpecification();
    }

    @RequestMapping("findBySortPageBySpecification")
    public List<Order>findBySortPageBySpecification(){
        return service.findBySortPageBySpecification();
    }

    @RequestMapping("findOrderByMyReposity/{id}")
    public Object findOrderByMyReposity(@PathVariable("id") Long id){
        Order order = service.findOrderByMyReposity(id);
        System.out.println(order.getOrderDetails().size());
        return order.toString();
    }
}
